from gtts import gTTS
import os

def generate_audio_segment(text, filename):
    """
    根据文本生成音频文件。
    """
    tts = gTTS(text=text, lang='zh-cn') # 'zh-cn' 表示中文普通话
    tts.save(filename)
    print(f"音频片段 '{filename}' 已生成。")

# 生成不同的音频片段
txt2Voice =[("红色区域","red_area.mp3"),
            ("黄色区域","yellow_area.mp3"),
            ("橙色区域","orange_area.mp3"),
            ("蓝色区域","blue_area.mp3"),
            ("绿色区域","green_area.mp3"),
            ("1号仪表盘","dashboard_1.mp3"),
            ("2号仪表盘","dashboard_2.mp3"),
            ("3号仪表盘","dashboard_3.mp3"),
            ("4号仪表盘","dashboard_4.mp3"),
            ("5号仪表盘","dashboard_5.mp3"),
            ("6号仪表盘","dashboard_6.mp3"),
            ("7号仪表盘","dashboard_7.mp3"),
            ("8号仪表盘","dashboard_8.mp3"),
            ("正常","normal.mp3"),
            ("偏高","high.mp3"),
            ("偏低","low.mp3")]
storage_path = "audio/"
# 确保存储路径存在
if not os.path.exists(storage_path):
    os.makedirs(storage_path)
# 生成所有音频片段
for text, filename in txt2Voice:
    generate_audio_segment(text, storage_path+filename)

print("所有音频片段生成完毕。")